
package me.zhengjie.rest;

import me.zhengjie.annotation.Log;
import me.zhengjie.base.AjaxResult;
import me.zhengjie.domain.AppArea;
import me.zhengjie.service.AppAreaService;
import me.zhengjie.service.dto.AppAreaDto;
import me.zhengjie.service.dto.AppAreaQueryCriteria;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

/**
* @website https://el-admin.vip
* @author IQW
* @date 2022-07-13
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "责任区管理")
@RequestMapping("/api/area")
public class AppAreaController {

    private final AppAreaService appAreaService;

    @ApiOperation("林情概况-查询地图")
    @GetMapping(value = "/info")
    public AjaxResult info(@RequestParam(defaultValue = "0") int areaId,
                           @RequestParam(defaultValue = "") String name){
        AppAreaDto item = appAreaService.info(areaId,name);
        if(null == item){
            return AjaxResult.error("查询数据不存在");
        }
        return AjaxResult.success(item);
    }

    @ApiOperation("林情概况-组织体系")
    @GetMapping(value = "/plist")
    public AjaxResult plist(@RequestParam(defaultValue = "0") int areaId,
                            @RequestParam(defaultValue = "") String name,
                            @RequestParam(defaultValue = "") String keyword){
        List list = appAreaService.plist(areaId,name,keyword);
        return AjaxResult.success(list);
    }

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('appArea:list')")
    public void exportAppArea(HttpServletResponse response, AppAreaQueryCriteria criteria) throws IOException {
        appAreaService.download(appAreaService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询责任区")
    @ApiOperation("查询责任区")
    @PreAuthorize("@el.check('appArea:list')")
    public ResponseEntity<Object> queryAppArea(AppAreaQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(appAreaService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @GetMapping(value = "/list")
    @Log("查询责任区下拉")
    @ApiOperation("查询责任区下拉")
//    @PreAuthorize("@el.check('appArea:list')")
    public ResponseEntity<Object> queryAppArea(AppAreaQueryCriteria criteria ){
        return new ResponseEntity<>(appAreaService.getChildrenUserList(criteria),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增责任区")
    @ApiOperation("新增责任区")
    @PreAuthorize("@el.check('appArea:add')")
    public ResponseEntity<Object> createAppArea(@Validated @RequestBody AppArea resources){
        return new ResponseEntity<>(appAreaService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改责任区")
    @ApiOperation("修改责任区")
    @PreAuthorize("@el.check('appArea:edit')")
    public ResponseEntity<Object> updateAppArea(@Validated @RequestBody AppArea resources){
        appAreaService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除责任区")
    @ApiOperation("删除责任区")
    @PreAuthorize("@el.check('appArea:del')")
    public ResponseEntity<Object> deleteAppArea(@RequestBody Integer[] ids) {
        appAreaService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}